%% Estimate Plain VARs for Individual Countries
% by Jaromir Benes
% 
% Estimate three plain VARs for the individual countries. Store and save
% the estimated VAR objects as well as the VAR output data containing the
% estimated residuals.

%% Clear Workspace
%
% Clear workspace, close all figure windows, move to the top of the command
% screen, and check the IRIS version.

clear;
close all;
home;
irisrequired 20130401;

%% Load Input Data
%
% Load the struct `d` with three country sub-databases from a binary file
% prepared in `read_data`. You must run `read_data` at least once before
% this file to create the necessary mat file.

load read_data.mat d;

%% Estimate Plain VAR for Each Country
%
% Use the data to estimate a plain 2nd-order VAR for each of the three
% countries. Remember that the data are organised as subdatabases `d.Au`,
% `d.Ca`, and `d.No`. Store both the estimated VAR objects, `vau`, `vca`,
% `vno`, and the output data -- because they also contain residuals that
% will be reported later. Store the VAR output data again as country
% sub-databases in a struct `dvi`.
%
% To estimate an VAR,
%
% * <?VAR?> create an empty VAR object specifying the variable names;
%
% * <?estimate?> run the function `estimate` with an input
% database (which is expected to contain at least the variable names
% entered at the time of creating the empty VAR), an estimation range, and
% other options.
%
% * <?output?> capture the two output arguments returned by `estimate`: the
% estimated VAR object, and the output database reproducing the input data
% (on the estimation range) and containing also the estimated residuals.
%
% * <?residualnames?> The residuals are, by default, named `res_xxx` where
% `xxx` is the names of individual VAR variables: `res_infl`, `res_gap`,
% `res_int`, `res_dex`.
%
% See Jaromir's tutorial on VAR basics for more on working with VAR
% objects.

p = 2;
estrange = qq(1997,1) : qq(2012,2);
v = VAR({'infl','gap','int','dex'}); %?VAR?

dvi = struct();
[vau,dvi.Au] = estimate(v,d.Au,estrange,'order=',p,'comment=','Australia'); %?VAR? %?output?
[vca,dvi.Ca] = estimate(v,d.Ca,estrange,'order=',p,'comment=','Canada');
[vno,dvi.No] = estimate(v,d.No,estrange,'order=',p,'comment=','Norway');

vau %#ok<NOPTS>
vca %#ok<NOPTS>
vno %#ok<NOPTS>

dvi.Au %?residualnames?
dvi.Ca
dvi.No

%% Save Estimated VARs
%
% Save the estimated individual VARs, along with the output data, to a
% binary file `estimate_individual_countries.mat` for future use.

save estimate_individual_countries.mat vau vca vno dvi;

%% Help on IRIS Functions Used in This M-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help dates/qq
%    help VAR/Contents
%    help VAR/VAR
%    help VAR/estimate
